package com.oazon.moco.playlist;

import java.util.List;
import java.util.Vector;

import android.content.Context;
import android.util.Log;

import com.oazon.common.Logger;
import com.oazon.moco.bean.Metadata;

public class RadioPlayList extends PlayList<Metadata> {

	private static RadioPlayList rList = null;
	private String TAG = "RadioPlayList";

	private RadioPlayList(Context context) {
		historySize = 10;
		bufferSize = 0;
		dynamicSize = 10;
		maxSize = 10;
	}

	public static RadioPlayList getInstance(Context context) {
		if (rList == null) {
			rList = new RadioPlayList(context);
			return rList;
		}

		return rList;
	}

	@Override
	public synchronized boolean add(Metadata metaData) {
		if (mPlayList.size() == historySize) {
			removeFirst();
		}
		metaData.setRecommand(true);
		return mPlayList.add(metaData);
	}

	@Override
	public synchronized boolean addAll(List<Metadata> metadatas) {
		boolean result = false;
		for (Metadata md : metadatas) {
			md.setRecommand(true);
		}
		/*
		 * if (metadatas.size() >= historySize) { mPlayList.clear(); readIndex =
		 * 0; result = mPlayList.addAll(metadatas); } else { for (int i = 0; i <
		 * size()-historySize; i ++) { removeFirst(); } result =
		 * mPlayList.addAll(metadatas); }
		 */
		result = mPlayList.addAll(metadatas);
		dumpPlaylist();
		return result;
	}

	@Override
	public synchronized boolean addAll(int index, List<Metadata> metadatas) {
		return mPlayList.addAll(index, metadatas);
	}

	@Override
	public synchronized boolean add(int index, Metadata metaData) {
		mPlayList.add(index, metaData);
		return true;
	}

	@Override
	public synchronized Metadata remove(int location) {
		if (location >= mPlayList.size() || location < 0)
			return null;

		Metadata result = null;
		try {
			result = mPlayList.remove(location);
			Logger.d(TAG, "remove " + result.getTitle() + " isRecommand:"
					+ result.isRecommand());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Metadata removeFirst() {
		Metadata result = null;
		try {
			result = mPlayList.remove(0);
			Log.d(TAG,
					"remove " + result.getTitle() + " isRecommand:"
							+ result.isRecommand());
		} catch (Exception e) {
			e.printStackTrace();
		}

		dumpPlaylist();

		return result;
	}

	@Override
	public synchronized void clear() {
		mPlayList.clear();
	}

	@Override
	public int indexOf(Metadata metaData) {
		return mPlayList.indexOf(metaData);
	}

	@Override
	public Vector<Metadata> getPlayList() {
		return mPlayList;
	}

	@Override
	public int size() {
		return mPlayList.size();
	}

	public int calLastNoRecommanIndex() {
		int last = -1;
		for (int i = 0; i < mPlayList.size(); i++) {
			if (mPlayList.get(i) != null
					&& mPlayList.get(i).isRecommand() == true) {
				break;
			}
			last = i;
		}

		return last;
	}

	public void clearNoRecommanSongs() {
		int last = calLastNoRecommanIndex();
		Log.d(TAG, "clearNoRecommanSongs last:" + last);
		if (last == -1) {
			return;
		}

		for (int i = 0; i < last + 1; i++) {
			mPlayList.remove(0);
		}
	}

	public synchronized void update(int readindex) {
		if (readindex < 0 || readindex >= mPlayList.size())
			return;
		Log.d(TAG, "update readindex:" + readindex);
		for (int i = 0; i < readindex + 1; i++) {
			if (mPlayList.get(i) != null)
				mPlayList.get(i).setRecommand(false);
		}
		// syncDb(vector2List(mPlayList));
		// mPlaylistDao.saveAllPlayList(vector2List(mPlayList));
	}

	public void dumpPlaylist() {
		Log.d(TAG, "-----------------------");
		for (Metadata md : mPlayList) {
			String url = "";
			if (md.getRes() != null) {
				url = md.getRes().getValue();
			}
			Log.d(TAG, md.getTitle() + " Recommand:" + md.isRecommand()
					+ " url:" + url + "  provider:" + md.getServiceID());
		}
	}

}
